package models

import (
	"github.com/astaxie/beego/orm"
	"time"
)

type TodoProcess struct {
	Processid			int64
	Instanceid			int64
	Nodeid				int64
	Instancenodeid		int64
	Processcode			string
	Processname			string
	Nodename			string
	Createdate			time.Time
	Begindate			string
}

type TodoInstanceNodeCandidate struct {
	Processid				int64
	Instanceid				int64
	Nodeid					int64
	Instancenodeid			int64
	CandidateStatus			string
	Candidateid				string
	Createdate				time.Time
	Begindate				string
	Updatedate				time.Time
	Enddate					string
	Inputdata				string
	Formkey					string
	Nodename				string
	Finishdate				time.Time
	Processcode				string
}

type DefFlowInstanceTask struct {
	Processid				int64
	Instanceid				int64
	Taskid					int64
	Nodecode				string
	Nodeid					string
}

func GetDefFlowInstanceTask(task DefFlowInstanceTask) (DefFlowInstanceTask,error) {
	var param []interface{}
	sql := "SELECT " +
				" FIT.INSTANCE_ID INSTANCEID," +
				" FPN.PROCESSID PROCESSID, " +
				" FIT.ID TASKID, " +
				" FPN.ID NODEID, " +
				" FPN.CODE NODECODE " +
			" FROM " +
				" T_FLOW_INSTANCE_TASK FIT" +
			" LEFT JOIN " +
				" T_FLOW_PROCESS_NODE FPN " +
			"ON " +
				" FIT.NODE_ID = FPN.ID" +
			" where " +
				" 1 = 1"
			if task.Processid > 0 {
				sql += " AND FPN.PROCESSID = ? "
				param = append(param,task.Processid)
			}
			if task.Instanceid > 0 {
				sql += " AND FIT.INSTANCE_ID = ?"
				param = append(param,task.Instanceid)
			}
			if len(task.Nodecode) > 0 {
				sql += " AND FPN.CODE = ?"
				param = append(param,task.Nodecode)
			}
	var obj DefFlowInstanceTask
	o := orm.NewOrm()
	err := o.Raw(sql,param).QueryRow(&obj)
	return obj,err
}

// GetTSYSUSERById retrieves TSYSUSER by Id. Returns error if
// Id doesn't exist
func GetTodoProcessByCandidateId (candidateid int64,offset int64, limit int64) ([]*TodoProcess,int64,error) {
	o := orm.NewOrm()
	var list []*TodoProcess
	sql := "SELECT " +
			"N.PROCESSID ProcessId, "+
			"CA.INSTANCE_ID InstanceId, " +
			"N.NODE_ID NodeId, "+
			"N.ID INSTANCENODEID, " +
		    "PRO.CODE PROCESSCODE, " +
			"I.PROCESS_NAME Processname, " +
			"N.NODE_NAME NodeName, " +
			"CA.CreateDate " +
		" FROM " +
			" T_FLOW_INSTANCE_TASK_CANDIDATE CA " +
			" LEFT JOIN T_FLOW_INSTANCE_TASK N ON CA.NODE_ID = N.ID " +
			" LEFT JOIN T_FLOW_INSTANCE I ON CA.INSTANCE_ID = I.ID " +
			" LEFT JOIN T_FLOW_PROCESS PRO ON I.PROCESS_ID = PRO.ID " +
		" WHERE "+
			" CA.STATUS = 'Running' " +
			" AND CA.CANDIDATE_ID = ?" +
			" LIMIT ? OFFSET ?"
	count, err := o.Raw(sql, candidateid,limit,offset).QueryRows(&list)
	for _, data := range list {
		if !data.Createdate.IsZero() {

			data.Begindate = data.Createdate.Local().UTC().Format("2006-01-02 15:04:05")
		}
	}
	return list,count,err
}

func GetTodoInstanceNode(instanceId int64, instanceNodeId int64,candidateId int64)([]*TodoInstanceNodeCandidate,error)  {
 	sql := "SELECT " +
 				"FITF.FORMDATA FORMKEY," +
 				"CA.INPUT_DATA INPUTDATA," +
 				"CA.NODE_ID INSTANCENODEID," +
 				"CA.CREATEDATE CREATEDATE," +
 				"CA.FINISHDATE FINISHDATE," +
 		        "CA.INSTANCE_ID INSTANCEID," +
 				"FPN.NAME NODENAME," +
 				"FP.CODE PROCESSCODE," +
 				"FIN.NODE_ID NODEID" +
 		" FROM " +
 				"T_FLOW_INSTANCE_TASK_CANDIDATE CA " +
 		" LEFT JOIN " +
 				"T_FLOW_INSTANCE_TASK FIN" +
 		" ON " +
 				"CA.NODE_ID = FIN.ID " +
 		" LEFT JOIN " +
 				"T_FLOW_PROCESS_NODE FPN " +
 		" ON " +
 				"FIN.NODE_ID = FPN.ID" +
 		" LEFT JOIN " +
 				"T_FLOW_PROCESS FP " +
 		" ON " +
 				" FPN.PROCESSID = FP.ID" +
 		" LEFT JOIN " +
 				"T_FLOW_INSTANCE_TASK_FORM FITF " +
 		" ON " +
 				"CA.INSTANCE_ID = FITF.INSTANCEID " +
 			" AND " +
 				"CA.NODE_ID = FITF.INSTANCENODEID " +
 		" WHERE " +
 				"(CA.STATUS = 'Running' AND CA.CANDIDATE_ID = ? AND CA.INSTANCE_ID=? AND CA.NODE_ID =?) " +
 		"OR " +
 				"(CA.STATUS = 'Finished' AND CA.INSTANCE_ID = ?)" +
 		"ORDER BY " +
 			" CASE " +
 				" CA.STATUS " +
 			" WHEN " +
 				"'Running' " +
 			" THEN " +
 				"0 " +
 			"ELSE " +
 				"1 " +
 			"END " +
 				"ASC," +
 		" CA.CREATEDATE DESC"
	o := orm.NewOrm()
	var list []*TodoInstanceNodeCandidate
	_, err := o.Raw(sql, candidateId,instanceId,instanceNodeId,instanceId).QueryRows(&list)
	for _, data := range list {
		if !data.Createdate.IsZero() {
			data.Begindate = data.Createdate.Local().UTC().Format("2006-01-02 15:04:05")
		}
		if !data.Finishdate.IsZero() {
			data.Enddate = data.Finishdate.Local().UTC().Format("2006-01-02 15:04:05")
		}
	}
	return list,err
}
